
/**
 * Date:2019年10月29日上午11:21:59
 * Copyright (c) 2019, www.bwbroad.com All Rights Reserved.
 *
*/

package com.xue.common.core;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource;
import org.springframework.transaction.interceptor.RollbackRuleAttribute;
import org.springframework.transaction.interceptor.RuleBasedTransactionAttribute;
import org.springframework.transaction.interceptor.TransactionAttribute;
import org.springframework.transaction.interceptor.TransactionInterceptor;

/**
 * Description: 添加按方法名称拦截事务 <br/>
 * 在 ProxyTransactionManagementConfiguration 类中已经支持了注解事务
 * Date: 2019年10月29日 上午11:21:59 <br/>
 * 
 * @author xuejianxin
 */
@Configuration
public class TransactionAdviceConfig {
	@Autowired
	private PlatformTransactionManager transactionManager;

	@Bean("myTransactionInterceptor")
    public TransactionInterceptor myTransactionInterceptor() {
        NameMatchTransactionAttributeSource source = new NameMatchTransactionAttributeSource();
	    //只读事务，不做更新操作
	    RuleBasedTransactionAttribute readOnlyTx = new RuleBasedTransactionAttribute();
	    readOnlyTx.setReadOnly(true);
	    readOnlyTx.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS );
	    //当前存在事务就使用当前事务，当前不存在事务就创建一个新的事务
	    //RuleBasedTransactionAttribute requiredTx = new RuleBasedTransactionAttribute();
	    //requiredTx.setRollbackRules(
	    //  Collections.singletonList(new RollbackRuleAttribute(Exception.class)));
	    //requiredTx.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
	    RuleBasedTransactionAttribute requiredTx = new RuleBasedTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED,
	      Collections.singletonList(new RollbackRuleAttribute(Exception.class)));
	    //requiredTx.setTimeout(5);//设置超时
	    Map<String, TransactionAttribute> txMap = new HashMap<>();
	    txMap.put("*", readOnlyTx);//只读事务
	    txMap.put("save*", requiredTx);
	    txMap.put("insert*", requiredTx);
	    txMap.put("update*", requiredTx);
	    txMap.put("delete*", requiredTx);
	    source.setNameMap( txMap );
        return new TransactionInterceptor(transactionManager, source);
    }

	@Bean
	public BeanNameAutoProxyCreator transactionAutoProxy() {
		BeanNameAutoProxyCreator transactionAutoProxy = new BeanNameAutoProxyCreator();
		transactionAutoProxy.setProxyTargetClass(true);
		// 事物类以Service 或 ServiceImpl 结尾
		transactionAutoProxy.setBeanNames("*Service", "*ServiceImpl");
		transactionAutoProxy.setInterceptorNames("myTransactionInterceptor");
		return transactionAutoProxy;
	}
	
}
